<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <page-filter
      v-model:query="filterInfo.query"
      :clear-type="clearType"
      :filter-list="filterInfo.searchList"
      :filter-btn="filterInfo.searchBtn"
    >
      <template v-slot:filter-storeTime>
        <el-date-picker
          v-model="time"
          size="small"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="datetimerange"
          :shortcuts="$fn.pickerOptionsFn()"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="timeChange"
        ></el-date-picker>
      </template>
      <template v-slot:filter-code>
        <div class="scan-input-box">
          <el-input
            ref="codeInput"
            v-focus
            v-model="code"
            :clearable="true"
            style="width: 400px"
            placeholder="请扫码或输入发货单二维码"
            @keyup.enter.native="sacn"
            @input="(e) => (code = validSe(e))"
            @change="changeSacn"
          >
            <template #prepend>发货单二维码</template>
          </el-input>
        </div>
      </template>
    </page-filter>
    <!-- 条件栏 -->
    <table-container
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      :api="tableInfo.api"
      :query="filterInfo.query"
      v-model:data="tableInfo.data"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :check-box="checkBox"
      :isDynamic="false"
      @handleEvent="handleEvent"
    >
      <template v-slot:col-qty="scope">
        {{ $fn.Subtr(scope.row.qty, scope.row.receiveQty || 0) }}
      </template>
    </table-container>
  </page-dialog>
</template>
<script setup name="addBydeliverOrder">
import PageDialog from '@/components/PageDialog/index'
import PageFilter from '@/components/NewPageFilter/index'
import TableContainer from '@/components/TableContainer/index'
import { getReceivingOrderPage } from '@/api/stock'
import { getSupplierAll, getDeptAll, getDepotAll } from '@/api/core'
const { proxy } = getCurrentInstance()
const emit = defineEmits()

const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: true,
  },
  checkBox: {
    type: Boolean,
    default: false,
  },
  visible: {
    type: Boolean,
    default: false,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
})
const code = ref('')
// 过滤相关配置
const filterInfo = reactive({
  copyQuery: {},
  query: {
    confirmStatus: 0,
    supplierAuth: 'supplierId',
    depotAuth: 'depotId',
    deptAuth: 'deptId',
    storeTimeGreater: '',
    status: 3,
    storeTimeLess: '',
    sort: 'id desc',
  },
  searchList: [
    { slot: true, label: '请扫码', value: 'code' },
    {
      componentType: 'input',
      label: '发货单号',
      value: 'deliverOrderNo',
      event: search,
    },
    {
      componentType: 'input',
      label: '采购单号',
      value: 'purchOrderNo',
      event: search,
    },
    {
      componentType: 'input',
      label: '入库单号',
      value: 'orderNo',
      event: search,
    },
    {
      componentType: 'slip-select',
      label: '入库部门',
      value: 'deptIdMult',
      api: getDeptAll,
      query: { deptAuth: 'id', status: '1' },
      clearable: true,
      filterable: true,
      multiple: true,
      event: search,
    },
    {
      componentType: 'slip-select',
      label: '入库仓库',
      value: 'depotIdMult',
      api: getDepotAll,
      query: { depotAuth: 'id', status: '1' },
      clearable: true,
      filterable: true,
      multiple: true,
      event: search,
    },
    {
      slot: true,
      value: 'storeTime',
      label: '入库时间',
    },
    {
      componentType: 'slip-select',
      value: 'supplierIdMult',
      label: '供应商',
      api: getSupplierAll,
      query: { supplierAuth: 'supplierId', sort: 'create_time desc' },
      clearable: true,
      filterable: true,
      multiple: true,
      keyField: 'supplierName',
      valueField: 'supplierId',
      event: search,
    },
  ],
  searchBtn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
})

// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: getReceivingOrderPage,
  fieldList: [
    { label: '入库单号', value: 'orderNo' },
    { label: '发货单号', value: 'deliverOrderNo' },
    { label: '采购单号', value: 'purchOrderNo' },
    { label: '入库部门', value: 'deptName' },
    { label: '入库仓库', value: 'depotName' },
    { label: '入库时间', value: 'storeTime' },
    { label: '供应商', value: 'supplierName' },
    { label: '备注', value: 'remark' },
  ],
})
// 弹窗相关
const dialogInfo = reactive({
  title: '暂估入库单',
  width: '80%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    { label: '保存', event: handleSave, disabled: true, show: true },
  ],
})
const selectRow = ref({})
const time = ref('')
const clearType = ref('')
const constclearType = ref(true)

const shows = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emit('update:visible', newValue) // 更新父组件shows
  },
})

watch(
  () => filterInfo.query.storeTimeGreater,
  (val) => {
    if (!val) {
      time.value = null
    }
  }
)

filterInfo.copyQuery = proxy.$fn.deepClone(filterInfo.query)
getList()
function changeSacn(val) {
  console.log(val, 'val')
  if (!val) {
    filterInfo.query.purchOrderNo = ''
    filterInfo.query.deliverOrderNo = ''
  }
}
function sacn() {
  if (!code.value) {
    proxy.$message({
      message: '请扫码',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
    return
  } else {
    filterInfo.query.purchOrderNo = code.value.split('-')[0]
    filterInfo.query.deliverOrderNo = code.value.split('-')[1]
    search()
  }
}
function getList() {
  tableInfo.refresh = Math.random()
}
function search() {
  tableInfo.initCurpage = Math.random()
  tableInfo.refresh = Math.random()
}
function handleEvent(event, data) {
  if (event === 'tableClickRow') {
    if (data && data.id) {
      selectRow.value = data
      dialogInfo.btList[1].disabled = false
    }
  }
}
function handleSave() {
  shows.value = false
  emit('callback', {
    data: selectRow.value,
  })
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
}
function clearSearch() {
  filterInfo.query = proxy.$fn.deepClone(filterInfo.copyQuery)
  clearType.value = !clearType.value
  getList()
}
function timeChange(val) {
  if (val && val.length) {
    filterInfo.query.storeTimeGreater = val[0]
    filterInfo.query.storeTimeLess = val[1]
  } else {
    filterInfo.query.storeTimeGreater = ''
    filterInfo.query.storeTimeLess = ''
  }
}
</script>
<style scoped lang="scss"></style>
